
import Head from 'next/head'

<Head>
  <script>
    {
      `(function() {
         var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?e60fb290e204e04c5cb6f79b0ac1e697";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
       })();`
    }
  </script>
</Head>

![LangChain](https://pica.zhimg.com/50/v2-56e8bbb52aa271012541c1fe1ceb11a2_r.gif)





金属[#](#metal "此标题的永久链接")
========================

本笔记展示了如何使用[Metal](https://docs.getmetal.io/introduction)的检索器。

首先，您需要注册Metal并获取API密钥。您可以在[此处](https://docs.getmetal.io/misc-create-app)完成。

```python
# !pip install metal_sdk

```

```python
from metal_sdk.metal import Metal
API_KEY = ""
CLIENT_ID = ""
INDEX_ID = ""

metal = Metal(API_KEY, CLIENT_ID, INDEX_ID);

```

摄取文档[#](#ingest-documents "此标题的永久链接")
-------------------------------------

如果您尚未设置索引，则只需执行此操作。

```python
metal.index( {"text": "foo1"})
metal.index( {"text": "foo"})

```

```python
{'data': {'id': '642739aa7559b026b4430e42',
  'text': 'foo',
  'createdAt': '2023-03-31T19:51:06.748Z'}}

```

查询[#](#query "此标题的永久链接")
------------------------

现在我们的索引已经设置好，我们可以设置一个检索器并开始查询。

```python
from langchain.retrievers import MetalRetriever

```

```python
retriever = MetalRetriever(metal, params={"limit": 2})

```

```python
retriever.get_relevant_documents("foo1")

```

```python
[Document(page_content='foo1', metadata={'dist': '1.19209289551e-07', 'id': '642739a17559b026b4430e40', 'createdAt': '2023-03-31T19:50:57.853Z'}),
 Document(page_content='foo1', metadata={'dist': '4.05311584473e-06', 'id': '642738f67559b026b4430e3c', 'createdAt': '2023-03-31T19:48:06.769Z'})]

```

